package controller;

import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.User;
import service.UserService;
import util.Laydata;
import util.Page;

import java.util.List;

@Controller
@RequestMapping("/asyncuser")
public class AsyncUserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/tolist")
    public String toList(){
        return "asyncuser/list";
    }

    @RequestMapping("/list")
    @ResponseBody//区分同步异步请求，异步请求要加@ResponseBody
    public String list(User user,@RequestParam(value = "page", defaultValue = "1") Integer currentPage,
                       @RequestParam(value = "limit", defaultValue = "10") Integer pageSize ,Model model){
        //查询数据
        int totalCount=userService.count(user);
        //计算偏移量
        int offset=(currentPage-1)*pageSize;
        List<User> users = userService.findAll(user, offset, pageSize);
        Laydata laydata=new Laydata(0,"查询成功",totalCount, users);

        //打开了页面没有数据，不会自动执行该方法
        return JSON.toJSONString(laydata);
    }
   @RequestMapping("/toadd")
    public String toAdd(){
        return "asyncuser/add";
    }
    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(User user){
        int result = userService.addUser(user);
        if(result>0) {
            return JSON.toJSONString(new Laydata(200,"添加成功"));
        }
        else{
            return JSON.toJSONString(new Laydata(500,"添加失败"));
        }
    }
//    @RequestMapping("/delete/{id}")
//    public String deleteById(@PathVariable("id") Integer id){
//        int result = userService.deleteUserById(id);
//        if(result>0){
//            return "redirect:/user/list/1";
//        }else{
//            return "error";
//        }
//    }
    //删除
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String deleteUser(@PathVariable("id") Integer id) {
        int result = userService.deleteUserById(id);  // 调用删除服务方法
        if (result > 0) {
            return JSON.toJSONString(new Laydata(0, "删除成功"));
        } else {
            return JSON.toJSONString(new Laydata(500, "删除失败"));
        }
    }
    //删除

    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model){
        User user = userService.findById(id);
        model.addAttribute("user", user);
        return "asyncuser/update";
    }

    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(User user){
        int result = userService.updateUser(user);
        if (result > 0) {
            return JSON.toJSONString(new Laydata(0, "更新成功"));
        } else {
            return JSON.toJSONString(new Laydata(500, "更新失败"));
        }
    }
    @RequestMapping("/toregist")
    public String toRegist(){
        return "regist";
    }



}
